package com.qqpz.sign_project.controller;

import com.google.gson.Gson;
import com.qqpz.sign_project.entity.*;
import com.qqpz.sign_project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/v1/records")
public class RecordsController {
    @Autowired
    private RecordsService recordsService;
    @Autowired
    private CoursesService coursesService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private UserService userService;
    @PostMapping(path = "/")
    public String createRecords(@RequestBody Records records){
        records.setId(UUID.randomUUID().toString());
        System.out.println(records);
        int i= 0;
        try {
            i = recordsService.createRecords(records);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    @GetMapping(path = "/")
    public String queryAllRecords(){
        List<Records> list = recordsService.queryAllRecords();
        List<User> users =new ArrayList<>();
        for (Records r :
                list) {
            User user = userService.queryUserById(r.getUserId());
            users.add(user);
        }
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("records",list);
            map.put("users",users);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @GetMapping(path = "/user/{userid}")
    public String queryByUserId(@PathVariable("userid") String userid){
        List<Records> list = recordsService.queryRecordsByUserId(userid);
        List<Courses> coursesList =new ArrayList<>();
        List<Location> locationList =new ArrayList<>();
        for (Records r :
                list) {
            Courses courses = coursesService.queryById(r.getCourseId());
            coursesList.add(courses);
            Location location = locationService.queryById(courses.getLocationId());
            locationList.add(location);
        }
        Map<String,Object> map=new HashMap<>();
        if(list!=null&&coursesList!=null&&locationList!=null){
            map.put("result","success");
            map.put("records",list);
            map.put("courses",coursesList);
            map.put("location",locationList);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @GetMapping(path = "/courses/{courseid}")
    public String queryByCourseId(@PathVariable("courseid") String courseid){
        List<Records> list = recordsService.queryRecordsByCourseId(courseid);
        List<User> users =new ArrayList<>();
        for (Records r :
                list) {
            User user = userService.queryUserById(r.getUserId());
            users.add(user);
        }
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("records",list);
            map.put("users",users);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @GetMapping(path = "/{userid}/{courseid}")
    public String queryByUserIdAndCourseId(@PathVariable("userid") String userid,@PathVariable("courseid") String courseid){
        Records list = recordsService.queryRecordsByUserIdAndCourseId(userid,courseid);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("records",list);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
//    @PostMapping(path = "/type")
//    @Transactional
//    public String updateType(@RequestBody Records records){
//        int i=recordsService.updateTypeByUidCid(records);
//        Map<String,Object> map=new HashMap<>();
//        if(i!=0){
//            map.put("result","success");
//            return new Gson().toJson(map);
//        }else{
//            map.put("result","fail");
//            return new Gson().toJson(map);
//        }
//    }
////    @PutMapping(path = "/status")
////    @Transactional
////    public String updateStatus(@RequestBody Records records){
////        Courses courses = coursesService.queryById(records.getCourseId());
////        User user = userService.queryUserById(records.getUserId());
////        Date endTime = courses.getEndTime();
////        Date now=new Date();
////        if (now.compareTo(endTime)>0){
////            records.setStatus(0);//签到失败
////            user.setPoints(user.getPoints()-1);//积分-1
////        }else {
////            records.setStatus(1);//签到成功
////            user.setPoints(user.getPoints()+1);//积分+1
////        }
////        int i=recordsService.updateStatusByUidCid(records);
////        int j=userService.updateUserById(user);
////        Map<String,Object> map=new HashMap<>();
////        if(i!=0 && j==1){
////            map.put("result","success");
////            return new Gson().toJson(map);
////        }else{
////            map.put("result","fail");
////            return new Gson().toJson(map);
////        }
////    }
//    @PostMapping(path = "/status")
//    @Transactional
//    public String updateStatus(@RequestBody Records records){
//        User user = userService.queryUserById(records.getUserId());
//        if (records.getStatus()==1){
//            user.setPoints(user.getPoints()+1);//签到积分+1
//        }
//        int j = userService.updateUserById(user);//更新user积分
//        int i=recordsService.updateStatusByUidCid(records);//更新签到情况
//        Map<String,Object> map=new HashMap<>();
//        if(i!=0 && j==1){
//            map.put("result","success");
//            return new Gson().toJson(map);
//        }else{
//            map.put("result","fail");
//            return new Gson().toJson(map);
//        }
//    }
//    @PostMapping(path = "/scores")
//    @Transactional
//    public String updateScores(@RequestBody Records records){
//        int i=recordsService.updateScoresByUidCid(records);
//        Map<String,Object> map=new HashMap<>();
//        if(i!=0){
//            map.put("result","success");
//            return new Gson().toJson(map);
//        }else{
//            map.put("result","fail");
//            return new Gson().toJson(map);
//        }
//    }
//    @PostMapping(path = "/comments")
//    @Transactional
//    public String updateComments(@RequestBody Records records){
//        int i=recordsService.updateCommentsByUidCid(records);
//        Map<String,Object> map=new HashMap<>();
//        if(i!=0){
//            map.put("result","success");
//            return new Gson().toJson(map);
//        }else{
//            map.put("result","fail");
//            return new Gson().toJson(map);
//        }
//    }
    @PostMapping(path = "/update")
    public String updateRecords(@RequestBody Records records){
        Records records1 = recordsService.queryRecordsByUserIdAndCourseId(records.getUserId(), records.getCourseId());
        records.setId(records1.getId());
        int i= 0;
        try {
            i = recordsService.updateRecordsById(records);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    //按id删除
    @PostMapping(path ="/delete")
    public String deleteRecordsById(@RequestBody Records records){
        int i = 0;
        try {
            i = recordsService.deleteRecordsById(records.getId());
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String, String> map = new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result"," fail");
            return new Gson().toJson(map);
        }
    }
}
